package cn.com.netedge.iot.frontend.datasave.utils;

import cn.com.netedge.iot.common.constants.MqConstants;
import cn.com.netedge.iot.common.mq.MqMessageListener;
import cn.com.netedge.iot.common.utils.BaseJmsMsgUtil;
import cn.com.netedge.iot.frontend.datasave.config.DataSaveConfig;
import cn.com.netedge.iot.fe.datasave.mq.*;
import cn.com.netedge.iot.frontend.datasave.mq.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@RefreshScope
public class JmsMsgUtil extends BaseJmsMsgUtil {
    private static final Logger logger = LoggerFactory.getLogger(JmsMsgUtil.class);
    private static final int RECONNECT_INTERVAL = 1000 * 60;

    @Override
    public void subscribeQueues() {
        List<String> queueList = new ArrayList<>();
        queueList.add(MqConstants.QUEUE_DATA_C_DAILY_MESSAGE);
        queueList.add(MqConstants.QUEUE_DATA_C_MONTHLY_MESSAGE);
        queueList.add(MqConstants.QUEUE_DATA_C_LOAD_MESSAGE);
        queueList.add(MqConstants.QUEUE_DATA_C_LOAD2_MESSAGE);
        queueList.add(MqConstants.QUEUE_DATA_C_ONLINE_MESSAGE);
        queueList.add(MqConstants.QUEUE_DATA_C_EVENT_MESSAGE);
        queueList.add(MqConstants.QUEUE_DATA_C_EVENT_COMMUNICATION_MESSAGE);
        queueList.add(MqConstants.QUEUE_DATA_C_EVENT_DISCONNECTOR_MESSAGE);
        queueList.add(MqConstants.QUEUE_DATA_C_EVENT_FRAUDDETECTION_MESSAGE);
        queueList.add(MqConstants.QUEUE_DATA_C_EVENT_POWERFAILURE_MESSAGE);
        queueList.add(MqConstants.QUEUE_DATA_C_EVENT_POWERQUALITY_MESSAGE);
        queueList.add(MqConstants.QUEUE_DATA_C_EVENT_STANDARD_MESSAGE);
        queueList.add(MqConstants.QUEUE_DATA_C_EVENT_VOLTAGEQUALITY_MESSAGE);
        subscribeQueue(queueList);
    }

    @Override
    public MqMessageListener getMessageListener(String queueName) {
        MqMessageListener listener;
        if (MqConstants.QUEUE_DATA_C_DAILY_MESSAGE.equals(queueName)) {
            listener = DataSaveConfig.appContext.getBean(DataCDailyMessageConsumer.class);
        } else if (MqConstants.QUEUE_DATA_C_MONTHLY_MESSAGE.equals(queueName)) {
            listener = DataSaveConfig.appContext.getBean(DataCMonthlyMessageConsumer.class);
        } else if (MqConstants.QUEUE_DATA_C_LOAD_MESSAGE.equals(queueName)) {
            listener = DataSaveConfig.appContext.getBean(DataCLoadMessageConsumer.class);
        } else if (MqConstants.QUEUE_DATA_C_LOAD2_MESSAGE.equals(queueName)) {
            listener = DataSaveConfig.appContext.getBean(DataCLoadSecondMessageConsumer.class);
        }else if (MqConstants.QUEUE_DATA_C_ONLINE_MESSAGE.equals(queueName)) {
            listener = DataSaveConfig.appContext.getBean(DataCOnlineMessageConsumer.class);
        }else if (MqConstants.QUEUE_DATA_C_EVENT_MESSAGE.equals(queueName)) {
            listener = DataSaveConfig.appContext.getBean(DataCEventMessageConsumer.class);
        }else if (MqConstants.QUEUE_DATA_C_EVENT_COMMUNICATION_MESSAGE.equals(queueName)) {
            listener = DataSaveConfig.appContext.getBean(DataCEventCommunicationMessageConsumer.class);
        }else if (MqConstants.QUEUE_DATA_C_EVENT_DISCONNECTOR_MESSAGE.equals(queueName)) {
            listener = DataSaveConfig.appContext.getBean(DataCEventDisconnectorMessageConsumer.class);
        }else if (MqConstants.QUEUE_DATA_C_EVENT_FRAUDDETECTION_MESSAGE.equals(queueName)) {
            listener = DataSaveConfig.appContext.getBean(DataCEventFrauddetectionMessageConsumer.class);
        }else if(MqConstants.QUEUE_DATA_C_EVENT_POWERFAILURE_MESSAGE.equals(queueName)){
            listener = DataSaveConfig.appContext.getBean(DataCEventPowerfailureMessageConsumer.class);
        }else if(MqConstants.QUEUE_DATA_C_EVENT_POWERQUALITY_MESSAGE.equals(queueName)){
            listener = DataSaveConfig.appContext.getBean(DataCEventPowerqualityMessageConsumer.class);
        }else if(MqConstants.QUEUE_DATA_C_EVENT_STANDARD_MESSAGE.equals(queueName)){
            listener = DataSaveConfig.appContext.getBean(DataCEventStandardMessageConsumer.class);
        }else if(MqConstants.QUEUE_DATA_C_EVENT_VOLTAGEQUALITY_MESSAGE.equals(queueName)){
            listener = DataSaveConfig.appContext.getBean(DataCEventVoltageQualityMessageConsumer.class);
        }else {
            logger.debug("queueName:{} not support subscribe");
            return null;
        }
        return listener;
    }


}
